//
//  Constants.m
//  DrivingWizard
//
//  Created by apple on 16/8/9.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "Constants.h"
@implementation Constants




+(void)setObject:(NSString *)keyname andValue:(id)obj
{
    [[NSUserDefaults standardUserDefaults] setObject:obj forKey:keyname];
    [[NSUserDefaults standardUserDefaults] synchronize];
}
+(id)getObject:(NSString *)keyname
{
    return [[NSUserDefaults standardUserDefaults] objectForKey:keyname];
}

+(NSString *)getTimeStamp
{

    
//    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
//    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
//    NSDate *date = [formatter dateFromString:@"2015-02-21 00:00:00"];
//    NSTimeInterval timeInterval = [date timeIntervalSince1970];
//    longDate = (long long int)timeInterval;
    
    NSTimeInterval time = [[NSDate date] timeIntervalSince1970]*1000;
    long long int date = (long long int)time;
    //    NSLog(@"%lld",date);
    return [NSString stringWithFormat:@"%lld",date];
}



+ (NSMutableString *)sortString:(NSMutableString *)srcStr
{
    NSInteger len = [srcStr length];
    for (NSInteger i=0; i<len-1; i++) {
        for (NSInteger j=0; j<len-i-1; j++) {
            unichar ch1 = [srcStr characterAtIndex:j];
            unichar ch2 = [srcStr characterAtIndex:j+1];
            if (ch1 > ch2) {
                srcStr = [NSMutableString stringWithFormat:@"%@",[srcStr stringByReplacingCharactersInRange:NSMakeRange(j, 1) withString:[NSMutableString stringWithFormat:@"%C", ch2]]];
                srcStr = [NSMutableString stringWithFormat:@"%@",[srcStr stringByReplacingCharactersInRange:NSMakeRange(j+1, 1) withString:[NSString stringWithFormat:@"%C", ch1]]];
            }
        }
    }
    return srcStr;
}


+(NSString *)genUUID
{
    CFUUIDRef uuid_ref = CFUUIDCreate(NULL);
    CFStringRef str_ref = CFUUIDCreateString(NULL, uuid_ref);
    CFRelease(uuid_ref);
    NSString *uuid = [NSString stringWithString:(__bridge NSString *)str_ref];
    CFRelease(str_ref);
    return uuid;
}

+ (NSString *)getTime {
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
        NSString *dataTime = [formatter stringFromDate:[NSDate date]];
        return dataTime;
}


+(id)jsonToObject:(NSData *)data
{
    NSError *error;
    id obj =[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
    if (error) {
        obj = nil;
    }
    return obj;
}
+ (BOOL) isMobile:(NSString *)mobileNumbel{
    /**
     * 手机号码
     * 移动：134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
     * 联通：130,131,132,152,155,156,185,186
     * 电信：133,1349,153,180,189,181(增加)
     */
    NSString * MOBIL = @"^1(3[0-9]|5[0-35-9]|8[025-9])\\d{8}$";
    /**
     10         * 中国移动：China Mobile
     11         * 134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
     12         */
    NSString * CM = @"^1(34[0-8]|(3[5-9]|5[017-9]|8[2378])\\d)\\d{7}$";
    /**
     15         * 中国联通：China Unicom
     16         * 130,131,132,152,155,156,185,186
     17         */
    NSString * CU = @"^1(3[0-2]|5[256]|8[56])\\d{8}$";
    /**
     20         * 中国电信：China Telecom
     21         * 133,1349,153,180,189,181(增加)
     22         */
    NSString * CT = @"^1((33|53|8[019])[0-9]|349)\\d{7}$";
    
    NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBIL];
    NSPredicate *regextestcm = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM];
    NSPredicate *regextestcu = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU];
    NSPredicate *regextestct = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT];
    
    if (([regextestmobile evaluateWithObject:mobileNumbel]
         || [regextestcm evaluateWithObject:mobileNumbel]
         || [regextestct evaluateWithObject:mobileNumbel]
         || [regextestcu evaluateWithObject:mobileNumbel])) {
        return YES;
    }
    
    return NO;
}
//身份证号
+ (BOOL) validateIdentityCard: (NSString *)identityCard
{
    BOOL flag;
    if (identityCard.length <= 0) {
        flag = NO;
        return flag;
    }
    NSString *regex2 = @"^(\\d{14}|\\d{17})(\\d|[xX])$";
    NSPredicate *identityCardPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex2];
    return [identityCardPredicate evaluateWithObject:identityCard];
}
+ (BOOL)isNotNil:(NSString *)string {
    if ([string isEqual:[NSNull null]] || string == nil || [string isEqualToString:@""]) {
        return NO;
    }else{
    return  YES;
    }
}

+ (BOOL)isNil:(NSString *)string {
    if ([string isEqual:[NSNull null]] || string == nil || [string isEqualToString:@""]) {
        return YES;
    }else{
    return  NO;
    }
}
// 获取当前视图控制器
+ (UIViewController *)activityViewController
{
    UIViewController* activityViewController = nil;
    
    UIWindow *window = [[UIApplication sharedApplication] keyWindow];
    if(window.windowLevel != UIWindowLevelNormal)
    {
        NSArray *windows = [[UIApplication sharedApplication] windows];
        for(UIWindow *tmpWin in windows)
        {
            if(tmpWin.windowLevel == UIWindowLevelNormal)
            {
                window = tmpWin;
                break;
            }
        }
    }
    
    NSArray *viewsArray = [window subviews];
    if([viewsArray count] > 0)
    {
        UIView *frontView = [viewsArray objectAtIndex:0];
        
        id nextResponder = [frontView nextResponder];
        
        if([nextResponder isKindOfClass:[UIViewController class]])
        {
            activityViewController = nextResponder;
        }
        else
        {
            activityViewController = window.rootViewController;
        }
    }
    if (@available(iOS 13.0, *))
       {
           window = [[[UIApplication sharedApplication] windows] objectAtIndex:0];
       }else{
           window = [UIApplication sharedApplication].keyWindow;
       }
    UIViewController *appRootVC = window.rootViewController;
    UIViewController *topVC = appRootVC;
    if (topVC.presentedViewController) {
        topVC = topVC.presentedViewController;
        return topVC;
    }
    
    return activityViewController;
}



@end
